package com.cqw.webauto.rpc;

import java.util.Iterator;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * @Description: 博客园第三方qq自助登录
 * @author cqw
 * @date 2021年01月08日 11:39:10
 */
@Component
public class WebAuto {

    public static WebDriver driver;
    String baseUrl = "https://ing.cnblogs.com";
    public static final String LOCAL_WEB_DRIVER_URL = "c:\\program files (x86)\\google\\chrome\\application\\chromedriver.exe";

    static {
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        System.setProperty("webdriver.chrome.driver", LOCAL_WEB_DRIVER_URL);
        driver = new ChromeDriver(options);
    }

    Queue<String> queue = new LinkedBlockingDeque<>(1);


    public static void main(String[] args){
        WebAuto webAuto = new WebAuto();
        webAuto.cnblogsQQlogin();
    }

    /**
     * 在闪存发说说
     */
    public void cnblogsPublish(){
        //指定浏览器大小，不影响工作
        driver.manage().window().setSize( new Dimension( 1280,800 ));
        WebElement shancun = driver.findElement(By.id("app_ing"));
        shancun.click();
        WebElement speak = driver.findElement(By.cssSelector("#ing_publish_content .ing_text"));
        String username = star();
        String prename = queue.poll();
        //过滤与上次获取同一个名称不发闪存
        System.out.println(username + " | " +prename);
        if( !StringUtils.hasLength(prename) || (StringUtils.hasLength(username) && !username.equals(prename)) ){
            System.out.println("true");
//            speak.sendKeys("@" + username);
            //发送新闻
            speak.sendKeys(JsoupWebText.getNewsText());
            queue.add(username);
        }else{
            System.out.println("false");
            queue.add(prename);
        }
        //发布
        WebElement publish = driver.findElement(By.id("btn_ing_publish"));
        publish.click();
        System.out.println("执行成功！");
    }
    /**
     * @Description: 博客园第三方qq自助登录
     * @author cqw
     * @date 2021年01月08日 12:13:35
     */
  public void cnblogsQQlogin() {
	  beforeMethod();
      driver.get(baseUrl+"/");
//      driver.manage().window().maximize();
      WebDriverWait wait = new WebDriverWait(driver, 60);
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

//      List<WebElement> inputBox = driver.findElements(By.cssSelector("#header_user_right a"));
//      inputBox.get(0).click();

      //qq登录
//      WebElement qq = driver.findElement(By.xpath("//img[@src='/images/oauth/QQ.png']"));
      WebElement qq = driver.findElement(By.xpath("//img[contains(@src,'QQ.png')]"));
//      WebElement qq = driver.findElement(By.cssSelector("img[href$='/images/oauth/QQ.png']"));
      qq.click();
      qqLogin();

      cnblogsPublish();
  }

  /**
   * @Description: qq登录
   * @author cqw
   * @date 2021年01月08日 12:09:55
   */
  private void qqLogin(){
	  String currentWindow = driver.getWindowHandle();
      String preWindowString = currentWindow;
      Set<String> handles=driver.getWindowHandles();
      Iterator<String> it=handles.iterator();
      while(it.hasNext()){
          if(currentWindow == it.next()){
              continue;
          }
          driver.switchTo().window(it.next());
          System.out.println(driver.findElement(By.id("accredit_site_link")).getText());
          driver.switchTo().frame(0);
      }
      WebElement login = driver.findElement(By.xpath("//a[@class='face']"));
      login.click();
      driver.switchTo().window(preWindowString);
  }

  /**
   *
   * @Description: 密码登录
   * @author cqw
   * @date 2021年01月08日 12:09:33
   */
  private void passLogin() {
    WebElement username = driver.findElement(By.id("mat-input-0"));
    username.sendKeys("xxxx");
    WebElement password = driver.findElement(By.id("mat-input-1"));
    password.sendKeys("xxxx");
    WebElement login = driver.findElement(By.cssSelector("button .mat-button-wrapper"));
    login.click();
  }

  public void beforeMethod() {

  }

  public void quit() {
      driver.close();
  }

    /**
     * 获取最新发表闪存用户名
     */
  public String star(){
      return driver.findElement(By.cssSelector("#feed_list ul .ing-author")).getText();
  }

}
